package com.fjwt.gz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.db.entity.SysUser;
import com.fjwt.gz.db.entity.SysUserAuth;
import com.fjwt.gz.core.utils.StringKit;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.service.mapper.SysUserAuthMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 操作员认证表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2020-06-13
 */
@Service
public class SysUserAuthService extends ServiceImpl<SysUserAuthMapper, SysUserAuth> {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private SysUserService sysUserService;

    /** 根据登录信息查询用户认证信息  **/
    public SysUserAuth selectByLogin(String agentNo, String identifier, Byte identityType, String sysType){
        return baseMapper.selectByLogin(agentNo, identifier, identityType, sysType);
    }

    /** 添加用户认证表 **/
    @Transactional
    public void addUserAuthDefault(Long userId, String telPhone, String pwdRaw, String sysType){


        // 若为空， 那么使用系统默认密码
        String passwordEncode = new BCryptPasswordEncoder().encode(StringUtils.defaultString(pwdRaw, sysConfigService.queryDefaultPasswordRaw() ));


        String salt = StringKit.getUUID(6); //6位随机数

        /** 手机号登录方式 */
        SysUserAuth record = new SysUserAuth();
        record.setUserId(userId);
        record.setCredential(passwordEncode);
        record.setSalt(salt);
        record.setIsUpdatePwd(1);
        save(record);
    }


    /** 重置密码,  如果 resetPassword = true && newPwd = null 那么使用的系统默认密码。   */
    @Transactional
    public void resetAuthInfo(Long resetUserId, String authLoginUserName, String telphone, boolean resetPassword, String newPwd, String sysType){
        //更改密码
        if(resetPassword){
            // 若为空， 那么使用系统默认密码
            String passwordEncode = new BCryptPasswordEncoder().encode(StringUtils.defaultString(newPwd, sysConfigService.queryDefaultPasswordRaw() ));

            //根据当前用户ID 查询出用户的所有认证记录
            List<SysUserAuth> authList = list(SysUserAuth.gw().eq(SysUserAuth::getUserId, resetUserId));
            for (SysUserAuth auth : authList) {
                if(StringUtils.isEmpty(auth.getSalt())){ //可能为其他登录方式， 不存在salt
                    continue;
                }
                SysUserAuth updateRecord = new SysUserAuth();
                updateRecord.setAuthId(auth.getAuthId());
                updateRecord.setCredential(passwordEncode);
                if(null == newPwd){
                    updateRecord.setIsUpdatePwd(1);
                }else{
                    updateRecord.setIsUpdatePwd(0);
                }
                updateById(updateRecord);
            }

            // 更新密码过期时间
            SysUser updateRecord = new SysUser();
            updateRecord.setSysUserId(resetUserId);
            sysUserService.resetSysUserPwdTime(updateRecord, newPwd);
            sysUserService.updateById(updateRecord);
        }
    }


    /** 查询当前用户密码是否正确 */
    public boolean validateCurrentUserPwd(String pwdRaw){

        //根据当前用户ID + 认证方式为 登录用户名的方式 查询一条记录
        SysUserAuth auth = getOne(SysUserAuth.gw()
                .eq(SysUserAuth::getUserId, GzUserDetails.getCurrentUserDetails().getSysUser().getSysUserId())
        );
        if(auth != null && new BCryptPasswordEncoder().matches(pwdRaw, auth.getCredential())){
            return true;
        }

        return false;
    }


}
